﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.DtoModel.Sys;
using VisionCloud.Domain.Filters;
using VisionCloud.Service.Sys;

namespace VisionCloud.Api.Controllers.Sys
{
    /// <summary>
    /// 权限控制器
    /// </summary>
    [SystemModule("权限控制器")]
    [ApiAuthorize(PolicyEnum.RequireRolePermission)]
    [ApiVersion("1.0")]
    [Route("api/sys/[controller]/[Action]")]
    [ApiExplorerSettings(GroupName = "权限控制器")]
    public class PermissionController : ApiBaseController
    {
        /// <summary>
        /// 权限服务
        /// </summary>
        public PermissionService PermissionService { get; set; }

        #region 角色权限

        /// <summary>
        /// 创建角色权限
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> CreateRolePermission(SysRolePermissionDto model)
        {
            return await TryCatchAsync(PermissionService.CreateRolePermission(model, TokenModel.YunUserGuid));
        }

        /// <summary>
        /// 编辑角色权限
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> EditRolePermission(SysRolePermissionDto model)
        {
            return await TryCatchAsync(PermissionService.UpdateRolePermission(model, TokenModel.YunUserGuid));
        }

        /// <summary>
        /// 删除角色权限
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> DeleteRolePermission(SysRolePermissionDto model)
        {
            return await TryCatchAsync(PermissionService.DeleteRolePermission(model.RolePermissionId, TokenModel.YunUserGuid));
        }

        /// <summary>
        /// 获取角色权限
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetRolePermissionPage(SysRolePermissionCondition condition)
        {
            return await TryCatchAsync(PermissionService.GetRolePermissionPage(condition));
        }

        /// <summary>
        /// 获取一个或者多个角色的权限列表
        /// </summary>
        /// <param name="roleIdList"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetRolePermissionList(List<Guid> roleIdList)
        {
            return await TryCatchAsync(PermissionService.GetRolePermissions(roleIdList));
        }

        /// <summary>
        /// 删除用户的角色
        /// </summary>
        /// <param name="listSysUserRole"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> RomoveUsersRoles(List<SysUserRoleDto> listSysUserRole)
        {
            return await TryCatchAsync(PermissionService.RemoveUsersRoles(listSysUserRole, TokenModel.YunUserGuid));
        }

        /// <summary>
        /// 添加用户到角色
        /// </summary>
        /// <param name="listSysUserRole"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddUsersToRoles(List<SysUserRoleDto> listSysUserRole)
        {
            return await TryCatchAsync(PermissionService.AddUsersToRoles(listSysUserRole, TokenModel.YunUserGuid));
        }

        /// <summary>
        /// 获取角色的菜单列表
        /// </summary>
        /// <param name="roleIdList"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetRoleMenuList(List<Guid> roleIdList)
        {
            return await TryCatchAsync(PermissionService.GetRoleMenus(roleIdList));
        }

        /// <summary>
        /// 获取用户的菜单列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetUserMenuList(SysUserDto model)
        {
            return await TryCatchAsync(PermissionService.GetUserMenus(model.UserId));
        }

        /// <summary>
        /// 获取用户的权限列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetUserPermisionList(SysUserDto model)
        {
            return await TryCatchAsync(PermissionService.GetUserPermissions(model.UserId));
        }

        /// <summary>
        /// 获取所有的菜单树
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetMenuTree()
        {
            return await TryCatchAsync(PermissionService.GetMenuTree());
        }

        /// <summary>
        /// 获取角色的菜单树
        /// </summary>
        /// <param name="roleIdList"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetRoleMenuTree(RoleListDto roleIdList)
        {
            return await TryCatchAsync(PermissionService.GetRoleMenuTree(roleIdList.ListRoleId));
        }

        /// <summary>
        /// 获取用户的菜单树
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetUserMenuTree(SysUserDto model)
        {
            return await TryCatchAsync(PermissionService.GetUserMenuTree(model.UserId));
        }

        #endregion 角色权限
    }
}